package com.stu.view;

import com.stu.dao.AdminDao;
import com.stu.model.Admin;
import com.stu.util.StringUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Objects;

public class LoginFrame extends JFrame {

	private final JTextField usernametextField;
	private final JPasswordField passwordField;

	/**
	 * 登录
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(() -> {
			try {
				LoginFrame frame = new LoginFrame();
				frame.setVisible(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
	}

	public LoginFrame() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(LoginFrame.class.getResource("/img/登录icon.png")));
		setTitle("用户登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 585, 411);
		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel titlePanel = new JPanel();
		titlePanel.setBounds(0, 25, 567, 58);
		contentPane.add(titlePanel);
		
		JLabel titleLabel = new JLabel("学生管理系统登录");
		titleLabel.setIcon(new ImageIcon(Objects.requireNonNull(LoginFrame.class.getResource("/img/logo.png"))));
		titleLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 34));
		titlePanel.add(titleLabel);
		
		JPanel usernamePanel = new JPanel();
		usernamePanel.setBounds(61, 115, 437, 38);
		contentPane.add(usernamePanel);
		
		JLabel usernameLabel = new JLabel("用户名：");
		usernameLabel.setIcon(new ImageIcon(Objects.requireNonNull(LoginFrame.class.getResource("/img/用户.png"))));
		usernameLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		usernamePanel.add(usernameLabel);
		
		usernametextField = new JTextField();
		usernametextField.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		usernamePanel.add(usernametextField);
		usernametextField.setColumns(15);
		
		JPanel pswPanel = new JPanel();
		pswPanel.setBounds(61, 166, 437, 38);
		contentPane.add(pswPanel);
		
		JLabel pswLabel = new JLabel("密  码：");
		pswLabel.setIcon(new ImageIcon(Objects.requireNonNull(LoginFrame.class.getResource("/img/钥匙.png"))));
		pswLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		pswPanel.add(pswLabel);
		
		passwordField = new JPasswordField();
		passwordField.setColumns(15);
		passwordField.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		pswPanel.add(passwordField);
		
		JPanel loginBtnPanel = new JPanel();
		loginBtnPanel.setBounds(76, 217, 408, 38);
		contentPane.add(loginBtnPanel);
		
		JButton loginButton = new JButton("登录");
		loginButton.addActionListener(this::loginAction);
		loginButton.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		loginBtnPanel.add(loginButton);
	}
	/**
	 * 登录逻辑
	 */
	protected void loginAction(ActionEvent ae) {
	String userName = usernametextField.getText();
	//从用户名文本框获取文本
	String userPsw = passwordField.getText();
	//从密码文本框获取文本
	//UserType selectedItem = (UserType)usertypeComboBox.getSelectedItem() ;

	if (StringUtil.isEmpty(userName)) {
		JOptionPane.showMessageDialog(this, "用户名不能为空！");
		return;
	}
	if (StringUtil.isEmpty(userPsw)) {
		JOptionPane.showMessageDialog(this, "密码不能为空！");
		return;
	}
	AdminDao adminDao = new AdminDao();
	Admin adminTmp = new Admin();
	adminTmp.setName(userName);
	adminTmp.setPassword(userPsw);
	Admin admin = adminDao.login(adminTmp);
	if (admin == null) {
		JOptionPane.showMessageDialog(this, "用户名或密码有错误！");
		return;
	}
	JOptionPane.showMessageDialog(this, "登陆成功");
	this.dispose();//关闭当前窗口
	new MainFrame().setVisible(true);
	}
	
}
